﻿using System.Text;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Element;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler
{
	/// <summary>
	/// 配置节点的Handler基类
	/// </summary>
	public abstract class HandlerBase : IHandler
    {
        public HandlerBase ()
        {

        }

        /// <summary>
        /// Handler对应的Element
        /// </summary>
        public virtual ElementBase Element { get; set; }

        /// <summary>
        /// 根据PropertyValueType设置当前Handler的ParamObject对象
        /// </summary>
        /// <param name="rootParamObject"></param>
        /// <param name="parentParamObject"></param>
        /// <param name="context"></param>
        protected virtual object GetParamObject ( object rootParamObject, object parentParamObject, ITemplateRenderContext context, PropertyValueStyles valueStyles = PropertyValueStyles.NotSet )
        {
            var ele = this.Element as IContextElement;
            object paramObject = null;
            if ( valueStyles == PropertyValueStyles.NotSet )
            {
                valueStyles = ele.ValueStyle;
            }
            if ( valueStyles == PropertyValueStyles.None )
            {
                return null;
            }
            switch ( valueStyles )
            {
                case PropertyValueStyles.RootObject:
                    paramObject = rootParamObject;
                    break;

                case PropertyValueStyles.Context:
                    paramObject = context;
                    break;

                case PropertyValueStyles.NotSet:
                case PropertyValueStyles.Parent:
                    paramObject = parentParamObject;
                    break;
            }
            return paramObject;
        }

        /// <summary>
        /// 执行内容解析
        /// </summary>
        /// <param name="dom"></param>
        /// <param name="parent"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            this.DoRenderChildNodes( writer, rootParamObject, parentParamObject, context );
        }

        /// <summary>
        /// 执行子节点的DoRender
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="rootParamObject"></param>
        /// <param name="parentParamObject"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        protected virtual void DoRenderChildNodes ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            foreach ( var ele in this.Element.ChildNodes )
            {
                var handler = ele.Handler;
                if ( handler != null )
                {
                    handler.DoRender( writer, rootParamObject, parentParamObject, context );
                    if ( context.EndTemplate == true )
                    {
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public virtual void Dispose ()
        {
            this.Element = null;
        }
    }
}
